<?php
namespace App\Http\Controllers\Admin;
//店铺产品审核

use Illuminate\Http\Request;  
use App\Product;
use App\Shop;  

class ProductShenheController extends AdminController
{
    // 列表
    public function alist(Request $request)
    { 
        $status = trim($request->input('status', ''));
        $category_id = trim($request->input('category_id', ''));//业态
        $shop_id = trim($request->input('shop_id', ''));
        $shop_name = trim($request->input('shop_name', ''));
        
        $fliter = Product::where('member_id', '>', 0);
        
        
        if($status){
            $fliter = $fliter->where('shenhestatus', $status);
        }
        if($category_id){
            $fliter =  $fliter->whereIn('member_id', Shop::where('category_id', $category_id)->pluck('member_id'));
        }
        if($shop_id){
            $fliter =  $fliter->where('member_id', $shop_id);
        }
        if($shop_name){
            $fliter =  $fliter->whereIn('member_id', Shop::where('company', 'like', "%$shop_name%")->pluck('member_id'));
        }
        
        $this->itemsWithPage($request, $fliter);  
    }
    
    public function shenhe(Request $request){
        $status = trim($request->input('status', ''));//1:审核通过,3:审核不通过
        $status_extra = trim($request->input('status_extra', ''));
        $id = $request->input('id');//2:审核通过,3:审核不通过
        $product = Product::where('id', $id)->first();
        if(!$product){
            $this->err('id错误');
        }
        if($product->shenhestatus != 2){
            $this->err('已审核不能再次审核');
        }
        $product->shenhestatus = $status;
        $product->shehe_desription = $status_extra;
        if($product->save()){ 
            if($product->shenhestatus == 1){ 
                //发布时间已经过去，直接发布
                if($product->fabu_type == 1 || ($product->fabu_type == 2 && time() >= strtotime($product->dingshi_date) ) ){
                    $product->is_shelves = 1;
//                     $product->fabu_type = 3;
                    $product->fabu_date = date('Y-m-d H:i:s');
                    $product->save();
                }
            }
        }
        $this->output(array(), '审核完成');
    }
    
    //定时发布
    public static function DingShiFaBu(){
        
        error_log(chr(13).date('Y-m-d H:i:s').chr(13),3,'c:/aaa.log');
        
        
        Product::where('fabu_type', 2)->where('shenhestatus', 1)->where('is_shelves', 0)->where('dingshi_date', '<=', date('Y-m-d H:i:s'))->where('dingshi_date', '>', date('Y-m-d H:i:s', time()-60))
        ->update(['is_shelves' => 1, 'fabu_date'=>date('Y-m-d H:i:s')]);// 'fabu_type'=>3,
    }
}